﻿/// <summary>
///Author: Jignesh Chaudhari
///Written: 2013-Jan 21
///Purpose: Login class to handel login credential
/// </summary>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

namespace TravelExpertsASP
{
    public partial class Login : System.Web.UI.Page
    {

        bool isLoginFailed;
        protected void Page_Load(object sender, EventArgs e)
        {
            
            if (!IsPostBack)
            {
                isLoginFailed = false;
            }
            if (isLoginFailed)
            {
                  Response.Write("<script>alert('User name or password is incorrect. Please,try again.');</script>");
            
            }
            if (Session["isSessionSet"] != null)
            {
                if (Session["isSessionSet"] == "Flag")
                {
                    //Valid User
                    Response.Redirect("clientProfile.aspx");
                }
                else
                {
                    //not valid user
                   
                   
                }
            }
            else
            {
                
                //Session expired
             
               
            }
            
        }

        //logic button click event handler
        protected void btnLogin_Click(object sender, EventArgs e)
        {
            try
            {
                string uname = txtuserNm.Text.Trim(); //Get the username from the control
                string password = txtPassWord.Text.Trim(); //get the Password from the control
                DbAccess objDb = new DbAccess();
                bool flag = objDb.AuthenticateUser(uname, password);//calling databse method to see user is valid or not if valid it will return yes.
                if (flag == true)
                {
                    //if user is valid start session and set some session variables

                    isLoginFailed = false;
                    Session["isSessionSet"] = "Flag";
                    Session["CustEmail"]=uname;
                    Response.Redirect("clientProfile.aspx");
                   
                }
                    
                else
                {
                    isLoginFailed = true;
                 
                    lblError.Text = "User name or password is incorrect. Please,try again";
                    lblError.ForeColor = System.Drawing.ColorTranslator.FromHtml("#FF0000");
                    lblError.Style["font-weight"] = "bold";
                  
                    Response.Redirect("Login.aspx");

               

                }
            }
            catch (Exception )
            {
                throw;
            }
        }



    }
}